package com.honghu.villagepost.activity

import android.content.Context
import android.content.Intent
import androidx.recyclerview.widget.RecyclerView
import com.honghu.villagepost.App
import com.honghu.villagepost.adapter.NearSiteAdapter
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.base.BaseListActivity
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.entity.Store
import com.lzy.okgo.model.Response

/**
 * @author: mqwang
 * @create: 2/24/21 4:00 PM
 * @Description:
 */
class NearSiteActivity : BaseListActivity<Store>() {

    private var app: App? = null
    val nearSiteAdapter : NearSiteAdapter by lazy {
        NearSiteAdapter()
    }

    companion object{
        fun self(context: Context){
            val intent = Intent(context, NearSiteActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindRecyclerView(recLv: RecyclerView) {
        recLv.adapter = nearSiteAdapter
        app = application as App

        setMainTitle("附近网点")
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.logistics_location(this, if (app!!.getLocation() == null) 0.0 else app!!.getLocation()!!.latitude, if (app!!.getLocation() == null) 0.0 else app!!.getLocation()!!.longitude, object : DialogCallback<LzyResponse<MutableList<Store>>>() {
            override fun onSuccess(response: Response<LzyResponse<MutableList<Store>>>?) {
                onGetDataSuc(response!!.body()!!.data)
            }
        })
    }

    override fun getData(): MutableList<Store> {
        return nearSiteAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Store>?) {
        if (isRefresh)
            nearSiteAdapter.setNewData(list)
        else
            nearSiteAdapter.addData(list!!)
    }

}